package cn.jbolt.core.api;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.ToStringSerializer;

import java.util.ArrayList;
import java.util.List;

/**
 * ApiUser实现 javaBean
 *
 * @ClassName: ApiUserBean
 * @author: JFinal学院-小木 QQ：909854136
 * @date: 2020年3月17日
 */
public class JBoltApiUserBean implements JBoltApiUser {
	private Object userId;// 具体调用API的客户端用户表ID
	private Long onlineUserId;// 在线用户ID
	private String userName;// 客户端用户表username
	private boolean isSystemUser;// 当前设置的API UserId 是不是 系统内置jb_user用户
	private Long applicationId;// application的id字段
	private String sessionKey;// 小程序里sessionKey 或者pc的用户sessionId
	private boolean isTenant;//是否租户
	private String tenantSn;//租户SN

	private List<JBoltApiBindUserBean> bindUsers;// 关联其他用户表

	private String checkEffectiveToken;//用来检测有效性的token 紧急情况使用

	public String getCheckEffectiveToken() {
		return checkEffectiveToken;
	}

	public void setCheckEffectiveToken(String checkEffectiveToken) {
		this.checkEffectiveToken = checkEffectiveToken;
	}

	@JSONField(serializeUsing= ToStringSerializer.class)
	public Long getOnlineUserId() {
		return onlineUserId;
	}

	public JBoltApiUserBean setOnlineUserId(Long onlineUserId) {
		this.onlineUserId = onlineUserId;
		return this;
	}

	public JBoltApiUserBean() {
	}

	public JBoltApiUserBean(Long applicationId, Object userId, String userName,Long onlineUserId) {
		this(applicationId, userId, userName, false, onlineUserId);
	}

	public JBoltApiUserBean(Long applicationId, Object userId, String userName, String sessionKey,Long onlineUserId) {
		this(applicationId, userId, userName, false, sessionKey,onlineUserId);
	}
	public JBoltApiUserBean(Long applicationId, Object userId, String userName, boolean isSystemUser,Long onlineUserId) {
		this(applicationId, userId, userName, isSystemUser,null, onlineUserId);
	}

	public JBoltApiUserBean(Long applicationId, Object userId, String userName, boolean isSystemUser,Long onlineUserId,int bindUserType,Long bindUserId) {
		this(applicationId, userId, userName, isSystemUser,null, onlineUserId);
		this.addBindUser(bindUserType,bindUserId);
	}

	public void addBindUser(JBoltApiBindUserBean jBoltApiBindUserBean) {
		if(this.bindUsers == null){
			this.bindUsers = new ArrayList<>();
		}
		this.bindUsers.add(jBoltApiBindUserBean);
	}

	public void addBindUser(int bindUserType,Object bindUserId) {
		addBindUser(new JBoltApiBindUserBean(bindUserType,bindUserId));
	}

	public JBoltApiUserBean(Long applicationId, Object userId, String userName, boolean isSystemUser,
			String sessionKey,Long onlineUserId) {
		this.applicationId = applicationId;
		this.userId = userId;
		this.userName = userName;
		this.isSystemUser = isSystemUser;
		this.sessionKey = sessionKey;
		this.onlineUserId = onlineUserId;
	}

	/*
	 * public JBoltApiUserBean(Claims claims) { this.appId=claims.get("appId",
	 * Object.class); this.userId=claims.get("userId", Object.class);
	 * this.userName=claims.get("userName",String.class);
	 * this.sessionKey=claims.get("sessionKey",String.class); }
	 */
	public JBoltApiUserBean setUserId(Object userId) {
		this.userId = userId;
		return this;
	}

	public JBoltApiUserBean setUserName(String userName) {
		this.userName = userName;
		return this;
	}

	@Override
	@JSONField(serializeUsing= ToStringSerializer.class)
	public Object getUserId() {
		return userId;
	}

	@JSONField(serialize = false)
	@SuppressWarnings("unchecked")
	@Override
	public <T> T getUserIdAs() {
		return (T) userId;
	}
	@JSONField(serialize = false)
	@Override
	public Long getUserIdToLong() {
		Object userId = getUserId();
		return userId==null?null:Convert.toLong(userId.toString());
	}
	@JSONField(serialize = false)
	@Override
	public Integer getUserIdToInt() {
		Object userId = getUserId();
		return userId==null?null: Convert.toInt(userId.toString());
	}
	@JSONField(serialize = false)
	@Override
	public String getUserIdToStr() {
		Object userId = getUserId();
		return userId==null?null:userId.toString();
	}

	@Override
	public String getUserName() {
		return userName;
	}
	@Override
	@JSONField(serializeUsing= ToStringSerializer.class)
	public Long getApplicationId() {
		return applicationId;
	}

	public JBoltApiUserBean setApplicationId(Long applicationId) {
		this.applicationId = applicationId;
		return this;
	}
	@Override
	public String getSessionKey() {
		return sessionKey;
	}

	public JBoltApiUserBean setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
		return this;
	}
	@Override
	public List<JBoltApiBindUserBean> getBindUsers() {
		return bindUsers;
	}

	public JBoltApiUserBean setBindUsers(List<JBoltApiBindUserBean> bindUsers) {
		this.bindUsers = bindUsers;
		return this;
	}
	@Override
	public boolean getIsSystemUser() {
		return isSystemUser;
	}

	public void setIsSystemUser(boolean isSystemUser) {
		this.isSystemUser = isSystemUser;
	}
	@Override
	public boolean getIsTenant() {
		return isTenant;
	}

	public void setIsTenant(boolean isTenant) {
		this.isTenant = isTenant;
	}
	@Override
	public String getTenantSn() {
		return tenantSn;
	}

	public void setTenantSn(String tenantSn) {
		this.tenantSn = tenantSn;
	}
}
